//#include <QGuiApplication>
#include <QApplication>
#include <QQmlApplicationEngine>
#include "services/services.h"
#include "session/session.h"
#include <QDebug>
#include <QMetaType>
#include <QTranslator>
#include <QDir>
#include <QFont>
#include <QStandardPaths>

QString _dbc_driver="QSQLITE", _dbc_host="", _dbc_db="qml_ui_test.sqllite3.db", _dbc_user="", _dbc_passwd="";

int main(int argc, char *argv[])
{

    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QApplication app(argc, argv);

    QString app_data_dir_path = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation)[0];
    QString app_db_sys_path = QStandardPaths::standardLocations(QStandardPaths::DataLocation)[0] + "/" + _dbc_db;
    _dbc_db = app_data_dir_path + "/" + _dbc_db;
    QDir app_data_dir(app_data_dir_path);

    if(!app_data_dir.exists()) {
        app_data_dir.mkpath(app_data_dir_path);

    }
    if(!QFile::exists(_dbc_db)) {
        QFile app_db(app_db_sys_path);
        app_db.copy(_dbc_db);
    }

    QQmlApplicationEngine engine;

    Session ses;
    QTranslator translator;
    QQmlContext *context = engine.rootContext();
    Services svc(context, &ses);

    //QDir

    QFont font;
    font.setFamily("黑体");
    app.setFont(font);
    QObject::connect(&ses,SIGNAL(usernameChanged(QString)),&svc,SLOT(changeName(QString)));
    context->setContextProperty("Services",&svc);
    context->setContextProperty("Session",&ses);
    qRegisterMetaType<QSqlRelationalTableModel*>("LQSqlRelationalTableModel*");
    qRegisterMetaType<SqlQueryModel*>("SqlQueryModel*");

    if (translator.load(":/translations/lang_zh.qm")) {
           //注意这里路径不可以加qrc
           app.installTranslator(&translator);
           engine.retranslate();
           qDebug() << "load translator succeed!";
    }
    engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));

    if (engine.rootObjects().isEmpty())
        return -1;
    return app.exec();


}
